<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
    <script>
        // 保证 setData 函数在全局作用域中
        function setData(data) {
            if (data) {
                document.getElementById('userId').value = data.userId || '';
                document.getElementById('borrowId').value = data.borrowId || '';
                document.getElementById('userName').value = data.userName || '';
                document.getElementById('name').value = data.name || '';
                if (data.sex === '男'){
                    document.querySelector('input[name="sex"][value="男"]').checked = true;
                }else if (data.sex === '女'){
                    document.querySelector('input[name="sex"][value="女"]').checked = true;
                }
                document.getElementById('phone').value = data.phone || '';
            }
        }
    </script>
</head>
<body>
<div class="layui-form layuimini-form">
    <div class="layui-form-item" style="display: none">
        <label class="layui-form-label required">读者ID：</label>
        <div class="layui-input-block">
            <input type="text" id="userId" name="userId" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">读者卡号：</label>
        <div class="layui-input-block">
            <input type="text" id="borrowId" name="borrowId" lay-verify="required" lay-reqtext="图书编号不能为空" placeholder="请输入读者编号" value="" class="layui-input">
            <tip>填写读者编号。</tip>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">用户名：</label>
        <div class="layui-input-block">
            <input type="text" id="userName" name="userName" lay-verify="required" lay-reqtext="用户名不能为空" placeholder="请输入用户名" value="" class="layui-input">
            <tip>填写用户名。</tip>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">真实姓名：</label>
        <div class="layui-input-block">
            <input type="text" id="name" name="name" placeholder="请输入真实姓名" value="" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">性别：</label>
        <div class="layui-input-block">
            <input type="radio" name="sex" value="男" title="男">
            <input type="radio" name="sex" value="女" title="女">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">电话：</label>
        <div class="layui-input-block">
            <input type="text" id="phone" name="phone" class="layui-input" value="" placeholder="请输入读者电话">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
        </div>
    </div>
</div>
<script src="../../lib/jquery-3.4.1/jquery-3.4.1.min.js" charset="utf-8"></script>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer;

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            let user = {
                userId: data.field.userId,
                borrowId: data.field.borrowId,
                userName: data.field.userName,
                name: data.field.name,
                sex: data.field.sex,
                phone: data.field.phone,
                createdAt: new Date()
            }
            $.ajax({
                url: '/user/updateUser',
                type: 'POST',
                contentType: "application/json;charset=utf-8",
                data: JSON.stringify(user),
                success: function (res){
                    if (res.code === 200){
                        layer.msg("修改成功！",{icon: 1})
                    }else {
                        layer.msg('修改失败！',{icon: 2})
                    }
                }
            })
        });

    });
</script>
</body>
</html>